"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : hash分区表
Case Name   : 创建普通hash分区表，验证分区个数
Create At   : 2021/3/4
@zou_jialiang0501120110
Description :
    1、创建普通hash分区表,分区个数小于64
    2、创建普通hash分区表,分区个数等于64
    3、创建普通hash分区表,分区个数大于64
    4、清理环境
Expect      :
    1、创建hash分区表成功
    2、创建hash分区表成功
    3、创建hash分区表成功
    4、清理环境成功
History     :
    Modified by @zou_jialiang050 at 2022/12/06 优化用例支持大于64个分区
"""

import unittest
import os

from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH

logger = Logger()


class DdlTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.commonsh = CommonSH('PrimaryDbUser')
        self.tab_name = "tab_partition_hash"
        logger.info("-----检查数据库状态是否正常-----")
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_partition_hash(self):
        logger.info("-----step1：创建普通hash分区表,分区个数小于64 "
                    "expect：创建成功-----")
        param1 = ',\n'.join([f'''partition p{str(i)}''' for i in range(20)])
        create_cmd1 = f'''drop table if exists {self.tab_name}01;
            create table {self.tab_name}01(p_id int) 
            partition by hash(p_id) ({param1});'''
        logger.info(create_cmd1)
        create_res1 = self.commonsh.execut_db_sql(create_cmd1)
        logger.info(create_res1)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, create_res1)

        logger.info("-----step2：创建普通hash分区表,分区个数等于64 "
                    "expect：创建成功-----")
        param2 = ',\n'.join([f'''partition p{str(i)}''' for i in range(64)])
        create_cmd2 = f'''drop table if exists {self.tab_name}02;
            create table {self.tab_name}02(p_id int) 
            partition by hash(p_id) ({param2});'''
        create_res2 = self.commonsh.execut_db_sql(create_cmd2)
        logger.info(create_res2)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, create_res2)

        logger.info("-----step3：创建普通hash分区表,分区个数大于64 "
                    "expect：创建成功-----")
        param3 = ',\n'.join([f'''partition p{str(i)}''' for i in range(100)])
        create_cmd3 = f'''drop table if exists {self.tab_name}03;
                    create table {self.tab_name}03(p_id int) 
                    partition by hash(p_id) ({param3});'''
        create_res3 = self.commonsh.execut_db_sql(create_cmd3)
        logger.info(create_res3)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, create_res3)

    def tearDown(self):
        logger.info("-----step4：清理环境 expect：成功-----")
        clear_cmd = f"drop table {self.tab_name}01 cascade;" \
                    f"drop table {self.tab_name}02 cascade;" \
                    f"drop table {self.tab_name}03 cascade;"
        logger.info(clear_cmd)
        self.commonsh.execut_db_sql(clear_cmd)
        logger.info(f"-----{os.path.basename(__file__)} end-----")
